﻿package mortal.game.control
{
    import Framework.MQ.*;
    import Message.Command.*;
    import Message.Public.*;
    import com.gengine.global.*;
    import com.gengine.manager.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import extend.language.*;
    import flash.display.*;
    import mortal.common.global.*;
    import mortal.common.preLoadPage.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.net.rmi.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.alertwins.*;
    import mortal.game.view.msgbroad.*;
    import mortal.mvc.core.*;

    public class SystemController extends Controller
    {
        private var _alert:Sprite;
        private var _killUserView:KillUserView;
        private var _isOtherClient:Boolean = false;
        public var isSocketClose:Boolean = false;
        private var _SockCloseTimeOut:uint;

        public function SystemController()
        {
            return;
        }// end function

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ECmdCoreCommand._ECmdCoreKillUser, this.onKillUserHandler);
            Dispatcher.addEventListener(EventName.SocketClose, this.onSockCloseHandler);
            return;
        }// end function

        private function onSockCloseHandler(event:DataEvent) : void
        {
            if (!this._isOtherClient)
            {
                if (this._SockCloseTimeOut <= 0)
                {
                    this.isSocketClose = true;
                    this.socketClose();
                }
            }
            return;
        }// end function

        private function socketClose() : void
        {
            if (GameRMI.instance.isConnected == false)
            {
                if (this._alert == null)
                {
                    Alert.timerNO = false;
                    Alert.timerOut = 60;
                    Alert.buttonWidth = 80;
                    Alert.alertWinRenderer = TimerAlertWin;
                    Alert.mode = Alert.Mode_Simple;
                    this._alert = Alert.show(Language.getString(10012), null, Alert.OK, null, this.onCloseHandler);
                }
            }
            this._SockCloseTimeOut = 0;
            return;
        }// end function

        private function onCloseHandler(param1:int) : void
        {
            this._alert = null;
            if (param1 == Alert.OK)
            {
                BrowerManager.instance.reload();
            }
            return;
        }// end function

        private function onKillUserHandler(param1:MessageBlock) : void
        {
            var _loc_3:IssmNoticItem = null;
            var _loc_4:String = null;
            var _loc_5:String = null;
            var _loc_2:* = param1.messageBase as SAttribute;
            switch(_loc_2.value)
            {
                case EKickOutReason._EKickOutReasonByLockPlayer:
                {
                    if (GameLoadBar.hasInstance())
                    {
                        GameLoadBar.instance.hide();
                    }
                    this.alert(Language.getString(10004));
                    break;
                }
                case EKickOutReason._EKickOutReasonByGMOperation:
                {
                    this.alert(Language.getString(10013));
                    break;
                }
                case EKickOutReason._EKickOutReasonByErrorVersion:
                {
                    this.alert(Language.getString(10005));
                    break;
                }
                case EKickOutReason._EKickOutReasonByCloseGate:
                {
                    this.showKillView(EKickOutReason._EKickOutReasonByCloseGate);
                    break;
                }
                case EKickOutReason._EKickOutReasonByCloseCell:
                {
                    this.showKillView(EKickOutReason._EKickOutReasonByCloseCell);
                    break;
                }
                case EKickOutReason._EKickOutReasonByCloseGateMgr:
                {
                    this.showKillView(EKickOutReason._EKickOutReasonByCloseGateMgr);
                    break;
                }
                case EKickOutReason._EKickOutReasonByCloseDbApp:
                {
                    this.showKillView(EKickOutReason._EKickOutReasonByCloseDbApp);
                    break;
                }
                case EKickOutReason._EKickOutReasonByElseLogin:
                {
                    this.showKillView(EKickOutReason._EKickOutReasonByElseLogin);
                    this._isOtherClient = true;
                    break;
                }
                case EKickOutReason._EKickOutReasonByDbUpdateFail:
                {
                    this.showKillView(EKickOutReason._EKickOutReasonByDbUpdateFail);
                    break;
                }
                case EKickOutReason._EKickOutReasonByIssmThreeHour:
                {
                    _loc_4 = Language.getString(10010);
                    this.alert1(_loc_4);
                    _loc_3 = new IssmNoticItem();
                    _loc_3.updateData(_loc_4);
                    break;
                }
                case EKickOutReason._EKickOutReasonByIssmOfflineTimeLessFiveHour:
                {
                    _loc_5 = Language.getString(10011);
                    this.alert1(_loc_5);
                    _loc_3 = new IssmNoticItem();
                    _loc_3.updateData(_loc_5);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function alert1(param1:String, param2:uint = 4) : void
        {
            if (this._killUserView != null)
            {
                return;
            }
            if (this._alert == null)
            {
                Alert.mode = Alert.Mode_Simple;
                this._alert = Alert.show(param1, null, param2, Global.stage as Sprite, this.closeHandler1);
            }
            return;
        }// end function

        private function closeHandler1(param1:int) : void
        {
            this._alert = null;
            if (param1 == Alert.OK)
            {
                if (ParamsConst.instance.proctecUrl != null && ParamsConst.instance.proctecUrl != "")
                {
                    BrowerUtil.getUrl(ParamsConst.instance.proctecUrl, "_self");
                }
                else if (ParamsConst.instance.actimUrl != null && ParamsConst.instance.actimUrl != "")
                {
                    BrowerManager.instance.getUrl(ParamsConst.instance.actimUrl);
                }
            }
            return;
        }// end function

        private function alert(param1:String, param2:uint = 4) : void
        {
            if (this._killUserView != null)
            {
                return;
            }
            if (this._alert == null)
            {
                Alert.mode = Alert.Mode_Simple;
                this._alert = Alert.show(param1, null, param2, Global.stage as Sprite, this.closeHandler);
            }
            return;
        }// end function

        private function closeHandler(param1:int) : void
        {
            this._alert = null;
            if (param1 == Alert.OK)
            {
                if (ParamsConst.instance.actimUrl != null && ParamsConst.instance.actimUrl != "")
                {
                    BrowerManager.instance.getUrl(ParamsConst.instance.actimUrl);
                }
            }
            return;
        }// end function

        private function showKillView(param1:int) : void
        {
            if (this._alert != null)
            {
                return;
            }
            if (this._killUserView == null)
            {
                this._killUserView = new KillUserView();
            }
            this._killUserView.updateContentByType(param1);
            this._killUserView.show();
            return;
        }// end function

    }
}
